package com.baoxin.chenxixc4coach.fragments.teaching;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baoxin.chenxixc4coach.MyCallback;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.adapter.TeachVideoAdapter;
import com.baoxin.chenxixc4coach.bean.Video;
import com.baoxin.chenxixc4coach.fragments.TitleFragment;
import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class VideoFragment extends TitleFragment implements SwipeRefreshLayout.OnRefreshListener {
    private int type;//0 教学视频  1 我的视频
    private List<Video> videoList;
    private TeachVideoAdapter adapter;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;
    private Handler handler;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video, container, false);
        recyclerView = view.findViewById(R.id.recyclerview_video);
        refreshLayout = view.findViewById(R.id.srl);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeResources(R.color.colorAccent);
        adapter = new TeachVideoAdapter(activity);
        videoList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(activity,
                DividerItemDecoration.VERTICAL));

        request();

        return view;
    }

    @Override
    public String getTabTitle() {
        if (type == 0) {
            return "教学视频";
        } else {
            return "我的视频";
        }
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public void onRefresh() {

    }

    private void request() {

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        videoList = (List<Video>) msg.obj;
                        adapter.setData(videoList);
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        };

        JSONObject body = new JSONObject();
        try {
            body.put("page", 1);
            body.put("limit", 10);

            if (type == 0) {//不需要登录
                body.put("lable", "教学视频");
            } else {
                body.put("lable", "我的视频");
            }
            NetUtil.post(activity, Interfaces.URL_TEACH_VIDEOS, body.toString(), new MyCallback(Interfaces.URL_TEACH_VIDEOS, handler));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
